package com.ruoyi.member.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.MemberOrder;
import com.ruoyi.member.service.IMemberOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员订单Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/order")
public class MemberOrderController extends BaseController
{
    @Autowired
    private IMemberOrderService memberOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('member:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberOrder memberOrder)
    {
        startPage();
        List<MemberOrder> list = memberOrderService.selectMemberOrderList(memberOrder);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(memberOrderService.selectMemberOrderById(orderId));
    }

    /**
     * 修改订单状态
     */
    @PreAuthorize("@ss.hasPermi('member:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody MemberOrder memberOrder)
    {
        return toAjax(memberOrderService.updateOrderStatus(memberOrder));
    }

    /**
     * 同步订单状态
     */
    @PreAuthorize("@ss.hasPermi('member:order:sync')")
    @Log(title = "订单同步", businessType = BusinessType.UPDATE)
    @PostMapping("/sync/{orderId}")
    public AjaxResult syncOrder(@PathVariable("orderId") Long orderId)
    {
        return toAjax(memberOrderService.syncOrderStatus(orderId));
    }

    /**
     * 批量同步订单
     */
    @PreAuthorize("@ss.hasPermi('member:order:sync')")
    @Log(title = "批量同步订单", businessType = BusinessType.UPDATE)
    @PostMapping("/syncBatch")
    public AjaxResult syncBatchOrders()
    {
        return AjaxResult.success(memberOrderService.syncPendingOrders());
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('member:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberOrder memberOrder)
    {
        List<MemberOrder> list = memberOrderService.selectMemberOrderList(memberOrder);
        ExcelUtil<MemberOrder> util = new ExcelUtil<MemberOrder>(MemberOrder.class);
        util.exportExcel(response, list, "订单数据");
    }
}